import api from './api';

// 课程相关API服务

// 获取课程列表
export const getCourseList = () => {
  return api.get('/api/teacher/course/list');
};

// 创建课程
export const createCourse = (courseData: {
  name: string;
  class_name: string; 
  description: string;
}) => {
  return api.post('/api/teacher/course/create', courseData);
};

// 更新课程信息
export const updateCourse = (courseId: number, courseData: {
  name: string;
  class_name: string;
  description: string;
}) => {
  return api.put(`/api/teacher/course/update`, {
    id: courseId,
    ...courseData
  });
};

// 删除课程
export const deleteCourse = (courseId: number) => {
  return api.delete(`/api/teacher/course/delete?id=${courseId}`);
};

// 获取课程详情
export const getCourseDetail = (courseId: number) => {
  return api.get(`/api/teacher/course/detail?id=${courseId}`);
};

// 课程学生管理API

// 获取课程学生列表
export const getCourseStudents = (courseId: number) => {
  return api.get(`/api/teacher/course/student/list?course_id=${courseId}`);
};

// 添加学生到课程
export const addStudentsToCourse = (courseId: number, studentIds: number[], remark: string = '') => {
  return api.post('/api/teacher/course/student/add', {
    course_id: courseId,
    student_ids: studentIds,
    remark
  });
};

// 从课程移除学生
export const removeStudentFromCourse = (courseId: number, studentId: number) => {
  return api.delete('/api/teacher/course/student/remove', {
    data: {
      course_id: courseId,
      student_id: studentId
    }
  });
};

// 签到任务API

// 创建签到任务
export const createSignInTask = (taskData: {
  course_id: number;
  description: string;
  start_time: number; // Unix时间戳
  end_time: number; // Unix时间戳
  class_ids: number[];
}) => {
  return api.post('/api/teacher/sign_in/create', taskData);
};

// 获取签到任务列表
export const getSignInTaskList = (params?: {
  course_id?: number;
  status?: number; // 1-进行中，2-已完成，3-已取消
}) => {
  let url = '/api/teacher/sign_in/list';
  if (params) {
    const queryParams = [];
    if (params.course_id) queryParams.push(`course_id=${params.course_id}`);
    if (params.status) queryParams.push(`status=${params.status}`);
    if (queryParams.length > 0) {
      url += `?${queryParams.join('&')}`;
    }
  }
  return api.get(url);
};

// 获取签到任务详情
export const getSignInTaskDetail = (taskId: number) => {
  return api.get(`/api/teacher/sign_in/detail?id=${taskId}`);
};

// 获取签到记录
export const getSignInRecords = (taskId: number) => {
  return api.get(`/api/teacher/sign_in/records?task_id=${taskId}`);
};

// 手动为学生签到
export const manualSignIn = (taskId: number, studentId: number) => {
  return api.post('/api/teacher/sign_in/manual', {
    task_id: taskId,
    student_id: studentId
  });
};

// 获取签到统计数据
export const getSignInStatistics = (params?: {
  course_id?: number;
  class_id?: number;
  start_date?: string; // 格式：YYYY-MM-DD
  end_date?: string; // 格式：YYYY-MM-DD
}) => {
  let url = '/api/teacher/sign_in/statistics';
  if (params) {
    const queryParams = [];
    if (params.course_id) queryParams.push(`course_id=${params.course_id}`);
    if (params.class_id) queryParams.push(`class_id=${params.class_id}`);
    if (params.start_date) queryParams.push(`start_date=${params.start_date}`);
    if (params.end_date) queryParams.push(`end_date=${params.end_date}`);
    if (queryParams.length > 0) {
      url += `?${queryParams.join('&')}`;
    }
  }
  return api.get(url);
}; 